package cn.wolfcode.wolf2w.service.impl;

import cn.wolfcode.wolf2w.domain.UserInfo;
import cn.wolfcode.wolf2w.exception.LogicException;
import cn.wolfcode.wolf2w.redis.service.IUserInfoRedisService;
import cn.wolfcode.wolf2w.repository.UserRepository;
import cn.wolfcode.wolf2w.service.IUserInfoService;
import cn.wolfcode.wolf2w.util.AssertUtil;
import cn.wolfcode.wolf2w.util.Consts;
import cn.wolfcode.wolf2w.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;

import java.time.Duration;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

@Service
public class UserInfoServiceImpl implements IUserInfoService {

    @Autowired
    private UserRepository userRepository;

    @Autowired
    private IUserInfoRedisService userInfoRedisService;

    @Override
    public void save(UserInfo userInfo) {
        userRepository.save(userInfo);
    }

    @Override
    public void delete(String id) {
        userRepository.deleteById(id);
    }

    @Override
    public void update(UserInfo userInfo) {
        userRepository.insert(userInfo);
    }

    @Override
    public UserInfo get(String id) {
        return userRepository.findById(id).orElse(null);
    }

    @Override
    public List<UserInfo> list() {
        return userRepository.findAll();
    }

    @Override
    public boolean checkPhone(String phone) {
        return userRepository.findByPhone(phone) == null;
    }

    @Override
    public void sendVerifyCode(String phone) {
        userInfoRedisService.sendVerifyCode(phone);
    }


    /**
     * 注册用户
     * @param userInfo
     * @param verifyCode
     * @return
     */
    @Override
    public void regist(UserInfo userInfo, String rpassword, String verifyCode) {
        //String key = "sendVerifyCode_phone:"+userInfo.getPhone();
        //if (template.getExpire(key) == null){
        //    return new JsonResult(JsonResult.CODE_INVALID, "验证码已过期", null);
        //}
        //System.out.println("验证码有效期为: " + template.getExpire(key) + "s");
        //System.out.println("value: " + template.opsForValue().get(key));
        //if (!template.opsForValue().get(key).equals(verifyCode)){
        //    return new JsonResult(JsonResult.CODE_INVALID, "验证码错误", null);
        //}

        // 判断参数是否为空
        AssertUtil.hasLength(userInfo.getPhone(), "手机号不能为空");
        AssertUtil.hasLength(userInfo.getNickname(), "用户名不能为空");
        AssertUtil.hasLength(userInfo.getPassword(), "密码不能为空");
        AssertUtil.hasLength(rpassword, "确认密码不能为空");
        AssertUtil.hasLength(verifyCode, "验证码不能为空");

        // 校验2次密码是否一致
        AssertUtil.isEqual(userInfo.getPassword(), rpassword, "两次密码输入不一致");

        String code = userInfoRedisService.getVerifyCode(userInfo.getPhone());
        if(!verifyCode.equalsIgnoreCase(code)) {
            throw new LogicException("验证码失效或输入错误");
        }
        userInfo.setCity("");
        userInfo.setEmail("");
        userInfo.setLevel(1);
        userInfo.setHeadImgUrl("/images/default.png");
        userInfo.setInfo("");
        // 核心属性 自己控制
        userInfo.setState(0);
        //System.out.println("userInfo: " + userInfo);
        userRepository.save(userInfo);
    }

    @Override
    public UserInfo login(String username, String password) {
        UserInfo user = userRepository.findByPhone(username);
        if(user == null ||  !user.getPassword().equals(password)) {
            throw new LogicException("用户不存在或密码错误");
        }
        return user;
    }

    @Override
    public String setUserByToken(UserInfo userInfo) {
        return userInfoRedisService.setUserByToken(userInfo);
    }

    @Override
    public UserInfo getUserByToken(String token) {
        return userInfoRedisService.getUserByToken(token);
    }


}
